package kr.co.bettersoft.pushchat.adapter;

import java.util.List;

import kr.co.bettersoft.pushchat.activity.R;
import kr.co.bettersoft.pushchat.domain.PushChatChat;
import kr.co.bettersoft.pushchat.domain.PushChatDiary;
import kr.co.bettersoft.pushchat.fragment.ChattingListFragment;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * PushChatChatListAdapter
 *  채팅방 목록을 화면에 보여주기 위한 아답터
 * @author blue
 *
 */
public class PushChatChatListAdapter extends BaseAdapter {
	String TAG = "PushChatChatListAdapter";
	private Context context;
	private final List<PushChatChat> entries;
	private ChattingListFragment receiveChattingListFragment;

	public PushChatChatListAdapter(Context context, ChattingListFragment chattingListFragment, List<PushChatChat> entriesFn) {		
		this.context = context;
		this.entries = entriesFn;
		this.receiveChattingListFragment = chattingListFragment;
	}
	Boolean blued = false;
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listView;
		if (convertView == null) {
			listView = new View(context);
			listView = inflater.inflate(R.layout.adapter_pushchat_chatting_room_list, null);
		} else {
			listView = (View) convertView;
		}





		// set value into textview
		ImageView member_thumbnail_img = (ImageView) listView.findViewById(R.id.member_thumbnail_img);

		TextView chat_room_new_room_mark = (TextView) listView.findViewById(R.id.chat_room_new_room_mark);
		TextView chat_room_new_message_mark = (TextView) listView.findViewById(R.id.chat_room_new_message_mark);

		TextView member_nickname_txt = (TextView) listView.findViewById(R.id.member_nickname_txt);
		TextView chat_room_last_message_txt = (TextView) listView.findViewById(R.id.chat_room_last_message_txt);
		TextView chat_room_message_count_txt = (TextView) listView.findViewById(R.id.chat_room_message_count_txt);
		TextView date_txt = (TextView) listView.findViewById(R.id.date_txt);

		//		if(((PushChatChat)entries.get(position)).getThumbnail()==null){
		//			member_thumbnail_img.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));		// **	
		//		}else{
		member_thumbnail_img.setImageBitmap(((PushChatChat)entries.get(position)).getThumbnail());
		//		}

		member_nickname_txt.setText(((PushChatChat)entries.get(position)).getOtherUserNickName());
		chat_room_last_message_txt.setText(((PushChatChat)entries.get(position)).getContent());
		chat_room_message_count_txt.setText(((PushChatChat)entries.get(position)).getMessageCount());
		date_txt.setText(((PushChatChat)entries.get(position)).getRegisterDate());

		// 새 채팅방 / 안읽은 메시지
		if(((PushChatChat)entries.get(position)).getReadYn()==null||((PushChatChat)entries.get(position)).getReadYn().length()<1){			// 새로운 채팅방
			chat_room_new_message_mark.setVisibility(View.GONE);
			chat_room_new_room_mark.setVisibility(View.VISIBLE);
		}else if(((PushChatChat)entries.get(position)).getReadYn().equals("Y")){	// 모두 읽음. 모두 숨김
			chat_room_new_message_mark.setVisibility(View.GONE);
			chat_room_new_room_mark.setVisibility(View.GONE);
		}else if(((PushChatChat)entries.get(position)).getReadYn().equals("N")){	// 안읽음 메시지
			chat_room_new_message_mark.setVisibility(View.VISIBLE);
			chat_room_new_room_mark.setVisibility(View.GONE);
		}

		// 삭제 버튼
		View delete_line_layout = listView.findViewById(R.id.delete_line_layout);
		Button delete_line_btn = (Button) listView.findViewById(R.id.delete_line_btn);
		delete_line_btn.setTag(position);
		if(((PushChatChat)entries.get(position)).isShowCancelBtnYn()){
			delete_line_layout.setVisibility(View.VISIBLE);
		}else{
			delete_line_layout.setVisibility(View.GONE);
		}
		delete_line_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				receiveChattingListFragment.cancelPosition = Integer.parseInt(v.getTag()+"");
				receiveChattingListFragment.confirmAndDelete();
			}
		});

		//		ImageView thumbnailImg = (ImageView) listView.findViewById(R.id.member_thumbnail_img);
		//		TextView memberIdTxt = (TextView) listView.findViewById(R.id.member_id_txt);
		//		TextView memberCommentTxt = (TextView) listView.findViewById(R.id.member_comment_txt);
		//		if(((PushChatChat)entries.get(position)).getThumbnail()==null){
		//			thumbnailImg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));		// **	
		//		}else{
		//			thumbnailImg.setImageBitmap(((PushChatChat)entries.get(position)).getThumbnail());
		//		}
		//		memberIdTxt.setText(((PushChatChat)entries.get(position)).getNickName()+"("+((PushChatChat)entries.get(position)).getMemberId()+")");
		//		memberCommentTxt.setText(((PushChatChat)entries.get(position)).getComment());

		return listView;
	}

	@Override
	public int getCount() {
		return entries.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

}